package com.ruoyi.project.flink;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

public class UserRunner {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //kafka配置
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "192.168.33.207:9092");
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");

        //从Kafka获取数据流，这里使用UserInfoSchema反序列化数据
        DataStreamSource<User> stream = env.addSource(new FlinkKafkaConsumer<User>(
                "kafka",
                new UserInfoSchema(),
                properties
        )).setParallelism(1);

        stream.print("kafka").setParallelism(1);

        //写入MySQL
        stream.addSink(new UserSink()).setParallelism(1);

        env.execute();
    }


}
